package com.example.library;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.library.adminfragment.AdminSeatFragment;
import com.example.library.adminfragment.AdminUserFragment;
import com.example.library.adminfragment.AdminReportFragment;
import com.example.library.adminfragment.AdminSettingsFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class AdminMainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView bottomNavigationView;
    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_main);

        // 初始化数据库
        dbHelper = new DatabaseHelper(this);
        if (!dbHelper.checkDatabaseIntegrity(this)) {
            Toast.makeText(this, "数据库初始化中，请稍后...", Toast.LENGTH_LONG).show();
            dbHelper = new DatabaseHelper(this);
        }

        // 初始化底部导航栏
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        // 默认显示座位管理界面
        if (savedInstanceState == null) {
            loadFragment(new AdminSeatFragment());
            bottomNavigationView.setSelectedItemId(R.id.nav_seats);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        int itemId = item.getItemId();
        
        if (itemId == R.id.nav_seats) {
            fragment = new AdminSeatFragment();
        } else if (itemId == R.id.nav_users) {
            fragment = new AdminUserFragment();
        } else if (itemId == R.id.nav_statistics) {
            fragment = new AdminReportFragment();
        } else if (itemId == R.id.nav_settings) {
            fragment = new AdminSettingsFragment();
        }

        return loadFragment(fragment);
    }

    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragmentContainer, fragment)
                    .commit();
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
} 